#ifdef _OS_X_
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>	

#elif defined(WIN32)
#include <windows.h>
#include "GL/gl.h"
#include "GL/glu.h"
#include "GL/glut.h"

#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif

#include "Scene.h"
#include "RayTrace.h"
#include "Intersection.h"
#include "Ray.h"
#include "ColorReflect.h"









// -- Main Functions --
// - CalculatePixel - Returns the Computed Pixel for that screen coordinate
Vector RayTrace::CalculatePixel (int screenX, int screenY)
{
	if ((screenX < 0 || screenX > Scene::WINDOW_WIDTH - 1) ||
      (screenY < 0 || screenY > Scene::WINDOW_HEIGHT - 1))
   {
      // Off the screen, return black
      return Vector (0.0f, 0.0f, 0.0f);
   }
   /*
   -- How to Implement a Ray Tracer --

   This computed pixel will take into account the camera and the scene
   and return a Vector of <Red, Green, Blue>, each component ranging from 0.0 to 1.0

   /*In order to start working on computing the color of this pixel,
   you will need to cast a ray from your current camera position
   to the image-plane at the given screenX and screenY
   coordinates and figure out how/where this ray intersects with 
   the objects in the scene descriptor.
   The Scene Class exposes all of the scene's variables for you 
   through its functions such as m_Scene.GetBackground (), m_Scene.GetNumLights (), 
   etc. so you will need to use those to learn about the World.*/

	/*Calcular la recta de la camara*/
	
	Ray * rayo = Intersection::Calculate_Cam_Ray(screenX,screenY);

	ColorReflect * color1	=	Intersection::Do_Intersection_Shape(rayo,1);
	Vector color	=	color1->GetColor();

	if ( Scene::supersample ){
		color = color * (1.0f/5.0f);
		
		
			

		Ray * rayo2 = Intersection::Calculate_Cam_Ray(static_cast<float>( screenX )- 0.5f, static_cast<float>( screenY ) );

		ColorReflect * color2	=	Intersection::Do_Intersection_Shape(rayo2,1);
			
		color = color + color2->GetColor()* (1.0f/5.0f);

		delete rayo2;
		delete color2;

		Ray * rayo3 = Intersection::Calculate_Cam_Ray(static_cast<float>( screenX ), static_cast<float>( screenY ) + 0.5f);

		ColorReflect * color3	=	Intersection::Do_Intersection_Shape(rayo3,1);
			
		color = color + color3->GetColor()* (1.0f/5.0f);

		delete rayo3;
		delete color3;

		Ray * rayo4 = Intersection::Calculate_Cam_Ray(static_cast<float>( screenX )+ 0.5f, static_cast<float>( screenY ) );

		ColorReflect * color4	=	Intersection::Do_Intersection_Shape(rayo4,1);
			
		color = color + color4->GetColor()* (1.0f/5.0f);

		delete rayo4;
		delete color4;

		Ray * rayo5 = Intersection::Calculate_Cam_Ray(static_cast<float>( screenX ), static_cast<float>( screenY ) - 0.5f);

		ColorReflect * color5	=	Intersection::Do_Intersection_Shape(rayo5,1);
			
		color = color + color5->GetColor()* (1.0f/5.0f);

		delete rayo5;
		delete color5;
		
		
	}

	delete rayo;
	delete color1;
	//Vector dir = rayo->GetDirection();
	
	




  /* To determine if your ray intersects with an object in the scene, 
   you must calculate where your objects are in 3D-space [using 
   the object's scale, rotation, and position is extra credit]
   and mathematically solving for an intersection point corresponding to that object.*/


   /*For example, for each of the spheres in the scene, you can use 
   the equation of a sphere/ellipsoid to determine whether or not 
   your ray from the camera's position to the screen-pixel intersects 
   with the object, then from the incident angle to the normal at 
   the contact, you can determine the reflected ray, and recursively 
   repeat this process capped by a number of iterations (e.g. 10).*/

   /*Using the lighting equation & texture to calculate the color at every 
   intersection point and adding its fractional amount (determined by the material)
   will get you a final color that returns to the eye at this point.
   */

   
   // Until this function is implemented, return white
	return color;
   //return Vector ((dir.x+1)/2.0f,(dir.y+1)/2.0f, dir.z);
}




